<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="110px">
			<el-form-item label="检查名称:">
				<el-input v-model="queryParams.governCheckTaskName" placeholder="请输入检查名称" clearable
					@keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="检查日期:">
				<el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
					start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
			</el-form-item>
			<el-form-item label="检查类型:">
				<el-select v-model="queryParams.governCheckTaskTypeName" placeholder="请选择检查类型" clearable>
					<el-option v-for="value in taskTypeList" :key="value" :label="value" :value="value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-card class="card-box">
			<el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="40" align="center" fixed="left" />
				<el-table-column label="序号" width="55" type="index" align="center" prop="planId" fixed="left" />
				<el-table-column label="检查名称" align="center" prop="governCheckTaskName" :show-overflow-tooltip="true"
					width="350" />
				<el-table-column label="涉及危险源" align="center" prop="governInvolveDanger" :show-overflow-tooltip="true"
					width="150" />
				<el-table-column label="检查类型" align="center" prop="governCheckTaskTypeName" width="100" />
				<el-table-column label="计划开始日期" align="center" prop="governTaskStartTime" width="120" />
				<el-table-column label="计划结束日期" align="center" prop="governTaskEndTime" width="120" />
				<el-table-column label="登记时间" align="center" prop="governTaskRegisterTime" width="160" />
				<el-table-column label="检查单位" align="center" prop="governCheckCompany" width="100"
					:show-overflow-tooltip="true" />
				<el-table-column label="检查人员" align="center" prop="governCheckUserNames" width="160" />
				<el-table-column label="检查进度" align="center">
					<template v-slot="scope">
						{{ scope.row.completeCount + '/' + (scope.row.completeCount + scope.row.unCompleteCount) }}
					</template>
				</el-table-column>

				<el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width"
					fixed="right">
					<template #default="scope">
						<el-button type="text" icon="view" @click="handleView(scope.row)"
							v-hasPermi="['check:governCheckMine:view']">查看</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
				v-model:limit="queryParams.pageSize" @pagination="getList" />
		</el-card>

		<!-- 详情对话框 -->
		<el-dialog :title="title" v-model="detailOpen" width="55%" append-to-body>
			<el-form ref="planDetail" :model="formDetail" :rules="rules" label-width="150px" style="margin-right: 90px">
				<el-row v-if="isGovernCheck">
					<el-col :span="12">
						<el-form-item label="检查名称" prop="governCheckTaskName">
							<el-input v-model="formDetail.governCheckTaskName" placeholder="请输入检查名称" clearable
								:disabled="true" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="检查类型" prop="governCheckTaskTypeName">
							<el-select v-model="formDetail.governCheckTaskTypeName" placeholder="请选择检查类型" clearable
								:disabled=true style="width: 100%;">
								<el-option v-for="value in taskTypeList" :key="value" :label="value" :value="value" />
							</el-select></el-form-item>
					</el-col>
				</el-row>
				<el-row v-if="isGovernCheck">
					<el-col :span="12">
						<el-form-item label="检查来源">
							<el-input v-model="formDetail.governCheckSource" placeholder="请输入检查来源" :disabled="true" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="检查级别">
							<el-input v-model="formDetail.governCheckLevel" placeholder="请输入检查级别" clearable
								:disabled="true" />
						</el-form-item>
					</el-col>
				</el-row>


				<el-row v-if="isSelfCheck">
					<el-col :span="12">
						<el-form-item label="检查名称" prop="governCheckTaskName">
							<el-input v-model="formDetail.governCheckTaskName" placeholder="请输入检查名称" clearable />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="检查类型" prop="governCheckTaskTypeName">
							<el-select v-model="formDetail.governCheckTaskTypeName" placeholder="请选择检查类型"
								:disabled="true" clearable style="width: 100%;">
								<el-option v-for="value in taskTypeList2" :key="value" :label="value" :value="value" />
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>


				<el-row v-if="isSelfCheck">
					<el-col :span="12">
						<el-form-item label="检查范围">
							<el-input v-model="formDetail.checkRange" placeholder="请输入检查范围" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="检查级别">
							<el-select v-model="formDetail.governCheckLevel" placeholder="请选择检查级别" :disabled="true"
								clearable style="width: 100%;">
								<el-option v-for="value in governCheckLevelList" :key="value" :label="value"
									:value="value" />
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row v-if="isSelfCheck">
					<el-col :span="12">
						<el-form-item label="组织检查单位">
							<el-input v-model="formDetail.governCheckCompany" placeholder="请输入组织检查单位" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="被检查单位">
							<el-input v-model="formDetail.checkedCompany" placeholder="请输入被检查单位" clearable />
						</el-form-item>
					</el-col>
				</el-row>

				<el-row>
					<el-col :span="12">
						<el-form-item label="开始时间">
							<el-date-picker v-model="formDetail.governTaskStartTime" type="date" placeholder="选择日期"
								:disabled="true" style="width: 100%;" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="结束时间">
							<el-date-picker v-model="formDetail.governTaskEndTime" type="date" placeholder="选择日期"
								:disabled="true" style="width: 100%;" />
						</el-form-item>
					</el-col>
				</el-row>

				<el-row v-if="isSelfCheck">
					<el-col :span="24">
						<el-form-item label="检查种类">
							<el-select v-model="formDetail.checkKind" placeholder="请选择检查种类" clearable :disabled="true"
								style="width: 100%;">
								<el-option v-for="value in checkKindList" :key="value" :label="value" :value="value" />
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>


				<!-- <el-row v-if="isGovernCheck">
               <el-col :span="12">
                  <el-form-item label="检查名称:" prop="governCheckTaskName">
                     <el-input v-model="formDetail.governCheckTaskName" placeholder="请输入检查名称" clearable :disabled="true" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="涉及危险源:" prop="governInvolveDanger">
                     <el-input v-model="formDetail.governInvolveDanger" placeholder="请输入涉及危险源" :disabled="true" />
                  </el-form-item>
               </el-col>
            </el-row>
			
            <el-row v-if="isGovernCheck">
               <el-col :span="12">
                  <el-form-item label="检查类型:" prop="governCheckTaskTypeName">
                     <el-select v-model="formDetail.governCheckTaskTypeName" placeholder="请选择检查类型" clearable :disabled="true" style="width: 100%">
                        <el-option v-for="value in taskTypeList" :key="value" :label="value" :value="value" />
                     </el-select>
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="被检查单位:" prop="governCheckCompany">
                     <el-input v-model="formDetail.governCheckCompany" placeholder="请输入被检查单位" clearable :disabled="true" />
                  </el-form-item>
               </el-col>
            </el-row>
			
            <el-row>
               <el-col :span="12">
                  <el-form-item label="开始时间:">
                     <el-date-picker v-model="formDetail.governTaskStartTime" type="date" placeholder="选择日期" :disabled=true style="width: 100%" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="结束时间:">
                     <el-date-picker v-model="formDetail.governTaskEndTime" type="date" placeholder="选择日期" :disabled=true style="width: 100%" />
                  </el-form-item>
               </el-col>
            </el-row> -->

				<el-row>
					<el-col>
						<el-form-item label="检查人员:" prop="governCheckUserNames">
							<el-input v-model="formDetail.governCheckUserNames" placeholder="请选择检查人员" clearable
								:disabled="true" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>

			<el-card class="card-box">
				<el-table v-loading="loading" :data="governCheckTaskPlanItemList"
					@selection-change="handleSelectionChange">
					<el-table-column type="expand">
						<template #default="scope">
							<el-form label-position="left" inline>
								<el-row><el-form-item label="检查项目">
										<span> {{ scope.row.governCheckName + scope.row.governCheckContent }}</span>
									</el-form-item></el-row>
								<el-row><el-form-item label="检查方式">
										<span> {{ scope.row.governCheckWay }}</span></el-form-item></el-row>
								<el-row><el-form-item label="检查依据">
										<span> {{ scope.row.governCheckBasis }}</span></el-form-item></el-row>
								<el-row><el-form-item label="适用场合">
										<span> {{ scope.row.governApplicablePlace }}</span></el-form-item></el-row>
								<el-row><el-form-item label="检查人员">
										<span> {{ scope.row.governCheckUserName }}</span></el-form-item></el-row>
								<el-row><el-form-item label="检查时间">
										<span> {{ scope.row.governCheckTime }}</span></el-form-item></el-row>

							</el-form>
						</template>
					</el-table-column>
					<el-table-column label="序号" width="55" type="index" align="center" prop="taskId" />
					<el-table-column label="检查项目" align="center" :show-overflow-tooltip="true">
						<template #default="scope">
							{{ scope.row.governCheckName + scope.row.governCheckContent }}
						</template>
					</el-table-column>
					<!-- <el-table-column label="检查方式" align="center" prop="governCheckWay" width="180"
                  :show-overflow-tooltip="true" /> -->
					<!-- <el-table-column label="检查依据" align="center" prop="governCheckBasis" width="200"
                     :show-overflow-tooltip="true" />
                  <el-table-column label="适用场合" align="center" prop="governApplicablePlace" width="150"
                  :show-overflow-tooltip="true" />
                  <el-table-column label="检查人员" align="center" prop="governCheckUserName" width="100" />
                  <el-table-column label="检查时间" align="center" prop="governCheckTime" width="100" /> -->
					<el-table-column label="检查结果" align="center" prop="governCheckResult" width="80" fixed="right"
						:show-overflow-tooltip="true" />
					<el-table-column label="是否符合" align="center" width="80" fixed="right" :show-overflow-tooltip="true">
						<template #default="scope">
							{{ scope.row.governCheckConform === 0 ? '符合' : scope.row.governCheckConform === 1 ? '不符合' :
                        scope.row.governCheckConform === 2 ? '待定中' : ""
                     }}
						</template></el-table-column>
					<el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
						<template #default="scope">
							<el-button type="text" icon="document-checked" @click="operation(scope.row, 'check')"
								v-hasPermi="['check:governCheckMine:checked']"
								:disabled="scope.row.submitStatus === '2'">符合</el-button>
							<el-button type="text" icon="document-delete" @click="operation(scope.row, 'close')"
								v-hasPermi="['check:governCheckMine:close']"
								:disabled="scope.row.submitStatus === '2'">不符合</el-button>
							<el-button type="text" icon="bell" @click="operation(scope.row, 'opportunity')"
								v-hasPermi="['check:governCheckMine:opportunity']"
								:disabled="scope.row.governCheckResult == '已排查' || scope.row.submitStatus === '1' || scope.row.submitStatus === '2'">
								{{ scope.row.governCheckConform === 2 ? '取消待定' : '待定' }}
							</el-button>
						</template>
					</el-table-column>
				</el-table>
				<pagination v-show="total1 > 0" :total="total1" v-model:page="queryParams.pageNum1"
					v-model:limit="queryParams.pageSize1" @pagination="getTaskPlanItemList" />
			</el-card>
		</el-dialog>

		<!-- 隐患录入对话框 -->
		<el-dialog :title="title" v-model="hiddenOpen" width="55%" append-to-body>
			<el-form ref="addHidden" :model="formH" :rules="rules" label-width="150px" style="margin-right: 90px">
				<el-divider content-position="left">登记信息</el-divider>
				<el-row v-if="false">
					<el-col :span="12">
						<el-form-item label="登记人">
							<el-input v-model="formH.registerUserName" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="登记时间">
							<el-date-picker v-model="formH.governCreateDate" type="date" placeholder="选择日期"
								style="width: 100%" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="隐患名称:" prop="hiddenInventoryName">
							<el-input v-model="formH.hiddenInventoryName" placeholder="请输入隐患名称" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="隐患来源:" prop="hiddenSource">
							<el-select v-model="formH.hiddenSource" placeholder="请选择隐患来源" clearable
								style="width: 100%;">
								<el-option v-for="value in sourceList" :key="value" :label="value" :value="value" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="隐患分类:" prop="hiddenType">
							<el-select v-model="formH.hiddenType" placeholder="请选择隐患分类" clearable style="width: 100%;">
								<el-option v-for="value in hiddenTypeList" :key="value" :label="value" :value="value" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="隐患等级:" prop="hiddenLevel">
							<el-select v-model="formH.hiddenLevel" placeholder="请选择隐患等级" clearable style="width: 100%;">
								<el-option v-for="value in hiddenLevelList" :key="value" :label="value"
									:value="value" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="所属装置:" label-position="right">
							<el-select v-model="formH.deviceName" placeholder="请选择所属装置" clearable style="width: 100%;"
								@change="fChangeDevice">
								<el-option v-for="device in deviceList" :key="device.id" :label="device.deviceName"
									:value="device" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="所属部门:" label-position="right">
							<TreeSelect v-model="formH.deptName" :options="deptNameList" placeholder="请选择所属部门"
								@change="changeDept" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="排查级别:">
							<el-select v-model="formH.checkLevel" placeholder="请选择排查级别" clearable style="width: 100%;"
								:disabled="true">
								<el-option v-for="value in checkList" :key="value" :label="value" :value="value" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="排查类型:" prop="hiddenCheckType">
							<el-input v-model="formH.hiddenCheckType" placeholder="请输入排查类型" :disabled="true" />
							<!--         <el-select v-model="formH.hiddenCheckType" placeholder="请选择排查类型" clearable style="width: 100%;" :disabled="true">
                        <el-option v-for="value in taskTypeList" :key="value" :label="value" :value="value" />
                     </el-select> -->
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="危害类型:" prop="harmType">
							<el-select v-model="formH.harmType" placeholder="请选择危害类型" clearable style="width: 100%;">
								<el-option v-for="value in WeiHaiTypeList" :key="value" :label="value" :value="value" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="治理资金:" label-position="right">
							<el-input v-model="formH.hiddenManagingFunds" placeholder="请输入治理资金" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="隐患治理类型:" prop="dangerManageType">
							<el-select v-model="formH.dangerManageType" placeholder="请选择隐患治理类型" clearable
								style="width: 100%;">
								<el-option v-for="value in ZhiLiTypeList" :key="value" :label="value" :value="value" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="隐患治理期限:" prop="rectificationEndTime">
							<el-date-picker v-model="formH.rectificationEndTime" type="date" placeholder="选择日期"
								style="width: 100%;" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-form-item label="隐患描述:" prop="hiddenInfo">
					<el-input v-model="formH.hiddenInfo" type="textarea" :rows="3" placeholder="请输入隐患描述" />
				</el-form-item>
				<el-form-item label="形成原因:">
					<el-input v-model="formH.hiddenEntryBasis" type="textarea" :rows="3" placeholder="请输入形成原因" />
				</el-form-item>
				<el-form-item label="整改措施:">
					<el-input v-model="formH.hiddenRectificationMeasure" type="textarea" :rows="3"
						placeholder="请输入整改措施" />
				</el-form-item>
				<!--
            <el-form-item label="隐患图片:">
               <el-image style="width: 100px; height: 100px" :src="formH.hiddenFileUrl" />
            </el-form-item>
			-->

				<el-form-item label="检查项:" v-if="isGovernCheck">
					<el-select v-model="formH.governCheckTaskItemId" placeholder="请选择检查项" clearable style="width: 100%;"
						@change="governCheckChanged">
						<el-option v-for="item in governCheckNameList" style="width:1000px;" :key="item.id"
							:label="item.governCheckName + item.governCheckContent"
							:value="item.governCheckTaskItemId" />
					</el-select>
				</el-form-item>

				<el-form-item label="检查项:" v-if="isSelfCheck">
					<el-input v-model="formH.governCheckContent" type="textarea" :rows="3" placeholder="请输入检查项"
						:disabled="true" />
				</el-form-item>

				<el-form-item label="检查依据:" v-if="isGovernCheck">
					<el-input v-model="formH.governCheckBasis" type="textarea" :rows="3" placeholder="请输入检查依据"
						:disabled="true" />
				</el-form-item>
				<el-form-item label="检查方式:">
					<el-input v-model="formH.governCheckWay" type="textarea" :rows="3" placeholder="请输入检查方式"
						:disabled="true" />
				</el-form-item>
				<el-form-item label="适用场合:" v-if="isGovernCheck">
					<el-input v-model="formH.governApplicablePlace" type="textarea" :rows="3" placeholder="请输入适用场合"
						:disabled="true" />
				</el-form-item>

				<el-row v-if="isGovernCheck">
					<el-col :span="12">
						<el-form-item label="扣分:">
							<el-select v-model="formH.governCheckTaskItemScoreId" placeholder="请选择扣分"
								style="width: 100%;" @change="governCheckScoreChanged">
								<el-option v-for="item in deductPointList" :key="item.id"
									:label="item.governDeductPoint" :value="item.governCheckTaskItemScoreId" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="否决项:">
							<el-select v-model="formH.governIsReject" placeholder="请选择否决项" clearable
								style="width: 100%;">
								<el-option v-for="item in [{ id: 1, value: '是' }, { id: 0, value: '否' }]" :key="item.id"
									:label="item.value" :value="item.id" />
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-form-item label="扣分说明:" v-if="isGovernCheck">
					<el-input v-model="formH.governDeductIllustrate" type="textarea" :rows="3" placeholder="请输入扣分说明"
						:disabled="true" />
				</el-form-item>
				<el-row>
					<el-col :span="12">
						<el-form-item label="罚款金额:">
							<el-input v-model="formH.hiddenCheckPay" placeholder="请输入罚款金额" />
						</el-form-item>
					</el-col>
				</el-row>

				<el-form-item label="文件上传:" prop="fileId">
					<FileUpload :limit="1" :fileSize="30" v-model="files"></FileUpload>
				</el-form-item>
				<el-row>
					<el-col :span="12">
						<el-form-item label="整改人:" label-position="right" prop="rectificationUserName">
							<el-input v-model="formH.rectificationUserName" readonly placeholder="请选择整改人"
								@click="onInnerOpen('rectificationUser')" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="复核人:" label-position="right" prop="reviewUserName">
							<el-input v-model="formH.reviewUserName" readonly placeholder="请选择复核人"
								@click="onInnerOpen('reviewUser')" />
						</el-form-item>
					</el-col>
				</el-row>
				<TableSelect ref="userTableSelect" :queryApi="listUser" :options="[]" :rowKey="'userId'"
					rowLabel="nickName" @onConfirm="onUserConfirm" rowLabelText="用户昵称" :maxNum="1">
					<template #formSlot="{ handleQuery, filterForm }">
						<el-col :span="8">
							<el-form-item label="部门名称">
								<el-input size="small" @change="handleQuery" v-model="filterForm.deptName" clearable
									style="width: 100%" @keyup.enter="handleQuery" />
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="用户名称">
								<el-input size="small" @change="handleQuery" v-model="filterForm.userName" clearable
									style="width: 100%" @keyup.enter="handleQuery" />
							</el-form-item>
						</el-col>
					</template>
					<template #tableSlot>
						<el-table-column label="部门" align="center" key="deptName" prop="dept.deptName" />
						<el-table-column label="用户名称" align="center" key="userName" prop="userName" />
						<el-table-column label="用户昵称" align="center" key="nickName" prop="nickName" />
					</template>
				</TableSelect>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button type="primary" @click="submitHiddenForm">保存并提交</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="GovernCheck">
	import {
		governCheckTaskItem,
		governCheckTaskItemId,
		governHiddenInventory,
		getCheckType,
		getDeviceLevel,
		getCheckLevel,
		getHiddenType,
		getHiddenLevel,
		getHiddenSource,
		getWeiHaiType,
		getZhiLiType
	} from "@/api/check/governCheckTaskInfo";
	import {
		listGovernUserCheck,
		getGovernCheck
	} from "@/api/check/governCheck";
	import {
		listUserSelfTaskPlanItem,
		updateGovernCheckTaskPlanItem
	} from "@/api/check/governCheckTaskPlanItem";
	import FileUpload from "@/components/FileUpload";
	import {
		deptTreeSelect,
		listUser
	} from "@/api/system/user";
	import TableSelect from "@/components/tableSelect/index";
	const files = ref([]);
	watch(
		() => files,
		(newData) => {
			if (newData.value.length === 0) {
				formH.value.hiddenFileId = "";
				formH.value.hiddenFileUrl = "";
				formH.value.hiddenFileName = "";
			} else {
				formH.value.hiddenFileName = newData.value[0].name;
				formH.value.hiddenFileId = newData.value[0].id;
				formH.value.hiddenFileUrl = newData.value[0].url;
			}
		}, {
			deep: true,
		}
	);

	const {
		proxy
	} = getCurrentInstance();

	const isGovernCheck = ref(false);
	const isSelfCheck = ref(false);

	const planList = ref([]);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const total1 = ref(0);
	const title = ref("");
	const title1 = ref("");
	const dateRange = ref([]);
	const taskTypeList = ref([]);


	const governTaskPlanId = ref();
	const governCheckTaskPlanItemList = ref([]);

	// 操作模态框
	const detailOpen = ref(false);

	// 隐患录入
	const hiddenOpen = ref(false);
	const deptNameList = ref([]);
	const deviceList = ref([]);
	const sourceList = ref([]);
	const checkList = ref([]);
	const hiddenLevelList = ref([]);
	const hiddenTypeList = ref([]);
	const WeiHaiTypeList = ref([]);
	const ZhiLiTypeList = ref([]);
	const userTableSelect = ref();
	const governCheckNameList = ref([]);
	const deductPointList = ref([]);

	const fChangeDevice = (e) => {
		formH.value.deviceId = e.id;
		formH.value.deviceName = e.deviceName;
	}

	const changeDept = (val, label) => {
		formH.value.deptName = label;
		formH.value.deptId = val;
	};
	const userType = ref("");
	const onInnerOpen = (type) => {
		userType.value = type;
		const {
			rectificationUserId,
			rectificationUserName,
			reviewUserId,
			reviewUserName,
		} = formH.value;
		let values = (rectificationUserId && rectificationUserId.split(",")) || [];
		let names = (rectificationUserName && rectificationUserName.split(",")) || [];
		if (type == "reviewUser") {
			values = (reviewUserId && reviewUserId.split(",")) || [];
			names = (reviewUserName && reviewUserName.split(",")) || [];
		}
		userTableSelect.value.open(
			values.map((item, index) => ({
				value: values[index],
				label: names[index],
			}))
		);
	};
	const onUserConfirm = (data) => {
		const values = data.map((item) => item.value);
		const names = data.map((item) => item.label);
		if (userType.value == "reviewUser") {
			formH.value.reviewUserId = values.join(",");
			formH.value.reviewUserName = names.join(",");
		} else {
			formH.value.rectificationUserId = values.join(",");
			formH.value.rectificationUserName = names.join(",");
		}
	};
	/** 隐患录入提交按钮 */
	function submitHiddenForm() {
		formH.value.hiddenFrom = isSelfCheck.value ? '企业自主检查' : '专项检查';
		proxy.$refs["addHidden"].validate(valid => {
			if (valid) {
				governHiddenInventory(formH.value).then(response => {
					proxy.$modal.msgSuccess("提交成功！");
					hiddenOpen.value = false;
					getTaskPlanItemList();
					taskNoPass(formH.value.governTaskPlanItemId);
				});
			}
		});
	}

	function taskNoPass(governTaskPlanItemId) {
		const param = {
			governTaskPlanItemId: governTaskPlanItemId,
			governCheckConform: 1
		}

		updateGovernCheckTaskPlanItem(param).then(response => {
			getTaskPlanItemList();
		});
	}

	const data = reactive({
		formDetail: {},
		formH: {
			deptNameList: [],
			deviceList: [],
			sourceList: [],
			checkList: [],
			hiddenLevelList: [],
			hiddenTypeList: [],
			WeiHaiTypeList: [],
			ZhiLiTypeList: [],
		},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			pageNum1: 1,
			pageSize1: 10,
			governCheckTaskName: undefined,
			governCheckCompany: undefined,
		},
		rules: {
			hiddenInventoryName: [{
				required: true,
				message: "隐患名称不能为空",
				trigger: "blur"
			}],
			hiddenSource: [{
				required: true,
				message: "隐患来源不能为空",
				trigger: "blur"
			}],
			hiddenType: [{
				required: true,
				message: "隐患分类不能为空",
				trigger: "blur"
			}],
			hiddenLevel: [{
				required: true,
				message: "隐患登记不能为空",
				trigger: "blur"
			}],
			hiddenCheckType: [{
				required: true,
				message: "排查类型不能为空",
				trigger: "blur"
			}],
			harmType: [{
				required: true,
				message: "危害类型不能为空",
				trigger: "blur"
			}],
			dangerManageType: [{
				required: true,
				message: "隐患治理类型不能为空",
				trigger: "blur"
			}],
			rectificationEndTime: [{
				required: true,
				message: "隐患治理期限不能为空",
				trigger: "blur"
			}],
			hiddenInfo: [{
				required: true,
				message: "隐患描述不能为空",
				trigger: "blur"
			}],
			rectificationUserName: [{
				required: true,
				message: "整改人不能为空",
				trigger: "blur"
			}],
			reviewUserName: [{
				required: true,
				message: "复核人不能为空",
				trigger: "blur"
			}],

			governCheckTaskName: [{
				required: true,
				message: "检查名称不能为空",
				trigger: "blur"
			}],
			governCheckCompany: [{
				required: true,
				message: "检查单位不能为空",
				trigger: "blur"
			}]
		},
	});

	const {
		queryParams,
		formDetail,
		formH,
		rules
	} = toRefs(data);

	/** 多选框选中数据 */
	function handleSelectionChange(selection) {
		ids.value = selection.map(item => item.governTaskPlanId);
		single.value = selection.length != 1;
		multiple.value = !selection.length;
	}

	/** 查询参数列表 */
	function getList() {
		loading.value = true;
		queryParams.value.governPlanStatus = 1;
		listGovernUserCheck(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
			planList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	/** 查看按钮操作 */
	function handleView(row) {
		reset();
		const id = row.governTaskPlanId || ids.value;
		getGovernCheck(id).then(response => {
			formDetail.value = response.data;
			let governCheckSource = formDetail.value.governCheckSource;
			if (governCheckSource && governCheckSource == '企业自主检查') {
				isSelfCheck.value = true;
				isGovernCheck.value = false;
			} else {
				isSelfCheck.value = false;
				isGovernCheck.value = true;
			}

			detailOpen.value = true;
			title.value = "检查任务";
		});
		governTaskPlanId.value = id;
		queryParams.value.pageNum1 = 1;
		getTaskPlanItemList();
	}

	/** 查看按钮操作 */
	function operation(row, type) {
		const params = {
			governTaskPlanItemId: row.governTaskPlanItemId,
			governCheckConform: type === 'check' ? 0 : type === 'close' ? 1 : row.governCheckConform === 2 ? 3 : 2
		}
		if (type === 'close') {
			resetHidden();
			files.value = [];
			hiddenOpen.value = true;
			title1.value = "隐患录入";
			const governCheckTaskId = row.governCheckTaskId;
			governCheckTaskItem({
				governCheckTaskId: governCheckTaskId
			}).then(response => {
				governCheckNameList.value = response.rows;
			});

			// alert(row.governCheckTaskItemId);
			formH.value.governTaskInfoId = row.governTaskInfoId;
			formH.value.governTaskPlanId = row.governTaskPlanId;
			formH.value.governTaskPlanItemId = row.governTaskPlanItemId;
			formH.value.governCheckTaskId = governCheckTaskId;
			formH.value.governCheckTaskItemId = row.governCheckTaskItemId;
			formH.value.governCheckContent = row.governCheckContent;

			let checkLevel = formDetail.value.governCheckLevel;
			formH.value.checkLevel = checkLevel ? checkLevel : "公司级";
			let checkTaskTypeName = formDetail.value.governCheckTaskTypeName;
			formH.value.hiddenCheckType = checkTaskTypeName ? checkTaskTypeName : "企业自查";

			formH.value.governCheckBasis = row.governCheckBasis;
			formH.value.governCheckWay = row.governCheckWay;
			formH.value.governApplicablePlace = row.governApplicablePlace;

			governCheckChanged();

		} else {
			updateGovernCheckTaskPlanItem(params).then(response => {
				getTaskPlanItemList();
			});
		}
	}

	/** 检查项改变事件 */
	function governCheckChanged() {
		deductPointList.value = [];
		governCheckTaskItemId(formH.value.governCheckTaskItemId).then(response => {
			if (response.data) {
				deductPointList.value = response.data;
				// 设置默认值
				formH.value.governCheckTaskItemScoreId = response?.data[0]?.governCheckTaskItemScoreId;
				formH.value.governDeductIllustrate = response?.data[0]?.governDeductIllustrate;
				formH.value.governDeductPoint = response?.data[0]?.governDeductPoint;
			} else {
				formH.value.governCheckTaskItemScoreId = undefined;
				formH.value.governDeductIllustrate = undefined;
				formH.value.governDeductPoint = undefined;
			}
		});
	}

	function governCheckScoreChanged(row) {
		formH.value.governCheckTaskItemScoreId = row.governCheckTaskItemScoreId;
		formH.value.governDeductIllustrate = row.governDeductIllustrate;
		formH.value.governDeductPoint = row.governDeductPoint;
	}
	/** 隐患录入表单重置 */
	function resetHidden() {
		formH.value = {};
		proxy.resetForm("addHidden");
	}
	/** 查询参数列表 */
	function getTaskPlanItemList() {
		loading.value = true;
		const params = {
			pageNum: queryParams.value.pageNum1,
			pageSize: queryParams.value.pageSize1,
			governTaskPlanId: governTaskPlanId.value,
		};

		listUserSelfTaskPlanItem(params).then(response => {
			governCheckTaskPlanItemList.value = response.rows;
			total1.value = response.total;
			loading.value = false;
		});

	}


	/** 表单重置 */
	function reset() {
		formDetail.value = {};
		proxy.resetForm("planDetail");
	}
	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}
	/** 重置按钮操作 */
	function resetQuery() {
		dateRange.value = [];
		proxy.resetForm("queryRef");
		handleQuery();
	}
	/** 查询检验类型列表 */
	function getDicList() {
		getCheckType().then(response => {
			taskTypeList.value = response.data;
		});
		deptTreeSelect().then((response) => {
			deptNameList.value = response.data;
		});
		getDeviceLevel().then(response => {
			deviceList.value = response.data;
		});
		getHiddenSource().then(response => {
			sourceList.value = response.data;
		});
		getCheckLevel().then(response => {
			checkList.value = response.data;
		});
		getHiddenLevel().then(response => {
			hiddenLevelList.value = response.data;
		});
		getHiddenType().then(response => {
			hiddenTypeList.value = response.data;
		});
		getWeiHaiType().then(response => {
			WeiHaiTypeList.value = response.data;
		});
		getZhiLiType().then(response => {
			ZhiLiTypeList.value = response.data;
		});
	}

	onMounted(() => {
		getDicList();
		getList();
	});
</script>